package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import client_gui.CampaignPatternGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.CampaignPattern;
import entity.Customer;
import entity.Employee;

/**
 * @class CampaignPatternController allows communication between the DB and marketing employee to generate campaign patterns
 * 
 * @param CampaignPatternGUI links to GUI.
 * @param CampaignPattern links to the object in the entity package
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingEmployeeController links to marketing employee controller.
 */
public class CampaignPatternController implements dbHandler,Serializable {
	private CampaignPatternGUI cpg;
	private CampaignPattern cp;
	private EmployeeLoginController elc;
	private MarketingEmployeeController mec;
	private Boss b;
	

public CampaignPatternController(CampaignPatternGUI cpg, EmployeeLoginController elc,MarketingEmployeeController mec){
	this.cpg=cpg;
	this.elc=elc;
	this.mec=mec;
	this.cpg.getNext0().addActionListener(new Next0());	
	this.cpg.getNext().addActionListener(new Next());		
	this.cpg.getNext2().addActionListener(new Next2());
	this.cpg.getBack().addActionListener(new Back());
	this.cpg.getSubmit().addActionListener(new Submit());
	getCpg().setexittlis(new Exit());

	this.getCpg().SetLogoutLis(new Logout());
	getProduct();
	getArea();
}

/**
 * function getArea() ask the server for Area names .
 */
private void getArea() {
	
	
	CampaignPattern cp = new CampaignPattern();
	this.setB(new Boss(cp,"getArea","CampaignPatternController"));
	this.Search();

	
}

/**
*  function getProduct() ask the server for product list .
*/
private void getProduct() {
	// 
	// TODO Auto-generated method stub
	CampaignPattern cp = new CampaignPattern();
	this.setB(new Boss(cp,"getProduct","CampaignPatternController"));
	this.Search();
	
}

public void create_boss(){
	
 
}

public CampaignPatternGUI getCpg() {
	return cpg;
}

public void setCpg(CampaignPatternGUI cpg) {
	this.cpg = cpg;
}

public EmployeeLoginController getElc() {
	return elc;
}

public void setElc(EmployeeLoginController elc) {
	this.elc = elc;
}

public MarketingEmployeeController getMec() {
	return mec;
}

public void setMec(MarketingEmployeeController mec) {
	this.mec = mec;
}

public void Add() {
	try {
		CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
}

@Override
public void Search() {
	try{CcrmClient.inet.setCurrCon(this);
	CcrmClient.inet.sendToServer(b);
		
	}catch (IOException ex){
		ex.printStackTrace();
	}
	
}

@Override
public String Delete() {
	// TODO Auto-generated method stub
	return null;
}

/**
 * the function HandleMsg get massage from server
 * @param msg it item from the DB Usually comes as Boss
 */
public void HandleMsg(Object msg) {
	this.setB((Boss) msg);
	if(this.getB().getMsg().equals("logout")){
		this.getCpg().dispose();
		this.getElc().getElg().setVisible(true);
	}
	else if(this.getB().getCommand().equals("getArea")){
		ArrayList <String> areas ;
		areas= (ArrayList<String>) this.getB().getObj();
		for(String i :areas)
			cpg.getArea().addItem(i); //change the attribute . 
	}

	else if(this.getB().getCommand().equals("getProduct")){
		ArrayList <String> areas ;
		areas= (ArrayList<String>) this.getB().getObj();
		for(String i :areas)
			cpg.getProduct().addItem(i); //change the attribute . 
	}
	else if(this.getB().getMsg().equals("pattern already exist")){
		cpg.getError0().setText("Pattern Already Exist"); 
		cpg.getError0().setForeground(Color.red);
		getCpg().getLblDefineNewId().setVisible(false);
		cpg.getError0().setVisible(true);
	}
	else if(this.getB().getMsg().equals("pattern added OK")){
		cpg.getError0().setText("Pattern Added Successfully"); 
		cpg.getError0().setForeground(Color.magenta);
		getCpg().getLblDefineNewId().setVisible(false);
		cpg.getError0().setVisible(true);
	}
		
}

@Override
public void update() {
	try {
		CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
public void setB(Boss b) {
	this.b = b;
}
public Boss getB() {
	return b;
}

public class Logout implements ActionListener{

	public void actionPerformed(ActionEvent e) {
		setB(new Boss(getElc().getEmp(),"logout","CampaignPatternController"));
		update();	
	}
	
}
public class Exit implements WindowListener{

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		
		setB(new Boss(getElc().getEmp(),"logout","CampaignPatternController"));
		update();
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
public class Next0 implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		setCp(new CampaignPattern());
		try {
			getCp().setId(Integer.parseInt(getCpg().getId().getText()));
			getCpg().getPanel1().setVisible(true);
		} catch (NumberFormatException e1) {
			getCpg().getLblDefineNewId().setVisible(false);
			getCpg().getError0().setVisible(true);
		}
	}	
}

/**
 * @function Next
 * choose product from catalog
 */
public class Next implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		getCp().setProduct(getCpg().getProduct().getSelectedItem().toString());
		getCpg().getPanel2().setVisible(true);
		
	}	
}

/**
 * @function Next2
 * sets customer info - age, living area, importance rating, interest rating.
 */
public class Next2 implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		
		try {
			cp.setFrom_age(cpg.getFrom_age().getText());
			cp.setTo_age(cpg.getTo_age().getText());
			cp.setArea(cpg.getArea().getSelectedItem().toString());
			cp.setFromImportance(Integer.parseInt(cpg.getFrom_importance().getText()));
			cp.setToImportance(Integer.parseInt(cpg.getTo_importance().getText()));
			cp.setFromInterest(Integer.parseInt(cpg.getFrom_interest().getText()));
			cp.setToInterest(Integer.parseInt(cpg.getTo_interest().getText()));
			getCpg().getPanel3().setVisible(true);
		} catch (NumberFormatException e1) {
			cpg.getLblDefineMarketSegment().setVisible(false);
			cpg.getError().setVisible(true);
		}
		////////////////     add permissions combo
				
	}	
}
public class Back implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		getCpg().dispose();
		getMec().getMeg().setVisible(true);			
	}	
}

public CampaignPattern getCp() {
	return cp;
}

public void setCp(CampaignPattern cp) {
	this.cp = cp;
}
public class Submit implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		try {
			getCp().setId(Integer.parseInt(getCpg().getId().getText()));
			cp.setFrom_age(cpg.getFrom_age().getText());
			cp.setTo_age(cpg.getTo_age().getText());
			cp.setArea(cpg.getArea().getSelectedItem().toString());
			cp.setFromImportance(Integer.parseInt(cpg.getFrom_importance().getText()));
			cp.setToImportance(Integer.parseInt(cpg.getTo_importance().getText()));
			cp.setFromInterest(Integer.parseInt(cpg.getFrom_interest().getText()));
			cp.setToInterest(Integer.parseInt(cpg.getTo_interest().getText()));
			cp.setMessageTextBox(cpg.getMessage().getText());
			b.setObj(cp);
			b.setControl("CampaignPatternController");
			b.setCommand("new pattern");
			Add();
		} catch (Exception e2) {
			// TODO: handle exception
		}
	}	
}


}
